package com.igoal.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.PotentialFriendDTO;
import com.igoal.dto.UserDTO;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.SearchResultModel;

@Controller
public class SearchController extends BaseController {	
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}
	
	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager) {
		this.userGoalManager = userGoalManager;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}
	
	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/search.html")
	public String search(@ModelAttribute("search") SearchResultModel search, HttpServletRequest request){
		if(Constants.SEARCH_FRIEND == search.getSearchType()){
			request.getSession().setAttribute("keyword", search.getKeyword());
			request.getSession().setAttribute("type", search.getSearchType());
			request.getSession().setAttribute("page", search.getPage());
			
			return "redirect:/search.html";
		}
		
		return "emptySearchResult";
	}

	
	@RequestMapping(method=RequestMethod.GET, value="/search.html")
	public String search(ModelMap modelMap, HttpServletRequest request){
		String keyword = request.getSession().getAttribute("keyword") == null ? "" : (String) request.getSession().getAttribute("keyword");
		int type = request.getSession().getAttribute("type") == null ? 1 : (Integer)request.getSession().getAttribute("type"); 
		SearchResultModel model = new SearchResultModel();
		model.setSearchType(type);
		model.setKeyword(keyword);
		model.setPage(0);
		model.setNextPage(-1);
		model.setPrevPage(-1);
		
		initRightPage(model, request);
		initLeftPage(model, request);
		initTopPage(model, request);
		
		request.getSession().removeAttribute("keyword");
		request.getSession().removeAttribute("type");
		request.getSession().removeAttribute("page");
		
		if(Constants.SEARCH_FRIEND == type){
			searchFriend(modelMap, model, keyword);
			return getViewPrefix() + "searchFriendResult";
		}
		
		applyModel(modelMap, model);
		
		return "emptySearchResult";
	}
	
	public void searchFriend(ModelMap modelMap, SearchResultModel model, String keyword){
		String username = getUserInSession();		
		if(StringUtils.isNotBlank(keyword)){			
			keyword = keyword.replaceAll("\\*", "").replaceAll("\\.", "").replaceAll("_", "");
			keyword = keyword.trim();
			if(StringUtils.isNotBlank(keyword)){
				List<PotentialFriendDTO> result = userManagerExt.searchFriend(username, keyword, 0);
				if(result != null){
					model.setResultOfFriendSearch(result);
					model.setCountResult(userManagerExt.countSearchResult(username, keyword));
					model.setHasNextPage(userManagerExt.checkSearchFriendResult(username, keyword, 1));
					if(model.getHasNextPage()){
						model.setNextPage(1);
					}
				}
			}
		}
		
		applyModel(modelMap, model);
	}

	public void initLeftPage(BaseModel model, HttpServletRequest request) {
		// TODO Auto-generated method stub

	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initTopZone(user, model);
		model.setUserId(user.getId());
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initRightZone(user, model);
		initPeopleYouMayKnow(user, model);
	}

}
